﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zor_Modes;
using Zor_Bll;
using Zor_Web.Models.Home;
using Zor_Tools;

namespace Zor_Web.Controllers
{
    [Authorization]
    public class HomeController : Controller
    {
        //
        // GET: /Index/

        #region - 登陆 -

        /// <summary>
        /// 登陆
        /// create by ternence at 2017.3.23
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        [AllowAnonymous]
        public ActionResult Index(string username, string password)
        {
            if (username != null)
            {
                BaseAnswer bar = Index_Bll<Sys_Users>.UserLogin(username, password);
                ViewBag.Success = bar.AnswerType;
                ViewBag.Message = bar.AnswerMeg;
                if (bar.AnswerType == BaseAnswer.Answers.Succee)
                    return RedirectToAction("Main", "Home");
                else
                    return View(bar);
            }
            else
            {
                ViewBag.Success = BaseAnswer.Answers.Succee;
                ViewBag.Message = "";
                return View();
            }

        }

        #endregion


        #region - 主框架 -

        /// <summary>
        /// 主框架
        /// </summary>
        /// <returns></returns>
        public ActionResult Main()
        {
            List<Sys_Navigations> lstMenu = Main_Bll<Sys_Navigations>.GetMenuList();

            V_Main vm = new V_Main();
            vm.Menu = lstMenu.Where(c => c.Nav_Level == 0).ToList<Sys_Navigations>();
            vm.NodeMenu = lstMenu.Where(c => c.Nav_ParentID == vm.Menu[0].Nav_Id).ToList<Sys_Navigations>();

            return View(vm);

        }


        /// <summary>
        /// 切换二级菜单
        /// </summary>
        /// <param name="fatherId">一级菜单ID</param>
        /// <returns></returns>
        public ActionResult NodeMenu(int fatherId)
        {
            var quer = GetSonId(fatherId);
            return Content(quer.ToList<Sys_Navigations>().ToJson());
        }


        /// <summary>
        /// linq 递归菜单
        /// </summary>
        /// <param name="id">父级菜单ID</param>
        /// <returns></returns>
        public IEnumerable<Sys_Navigations> GetSonId(int id)
        {
            var quer = from c in BaseClass.navigations where c.Nav_ParentID == id select c;
            return quer.ToList().Concat(quer.ToList().SelectMany(t => GetSonId(t.Nav_Id)));
        }

        #endregion

        #region - 修改密码 -
        
        /// <summary>
        /// 判断当前就密码是否存在
        /// </summary>
        /// <param name="oldPass"></param>
        /// <returns></returns>
        public ActionResult IsOldPassMapping(string oldPass)
        {
            BaseAnswer bar = new BaseAnswer();
            if (!string.IsNullOrEmpty(oldPass))
            {
                bar = Index_Bll<Sys_Users>.IsOldPassMapping(oldPass);
            }
            return Content(bar.ToJson());
        }

       /// <summary>
       ///  修改密码
       /// </summary>
       /// <param name="newPass"></param>
       /// <returns></returns>
        public ActionResult UpdatePass(string newPass)
        {
            BaseAnswer bar = new BaseAnswer();
            if (!string.IsNullOrEmpty(newPass))
            {
                bar = Index_Bll<Sys_Users>.UpdatePass(newPass);
            }
            return Content(bar.ToJson());
        }
        #endregion
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public ActionResult MainPage()
        {
            return View();
        }





    }
}
